package com.geostar.constructionland.analyse.service.handler.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyResultZphx;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.factory.AdjustModelFactory;
import com.geostar.constructionland.analyse.mapper.sptalyresult.AlyResultZphxMapper;
import com.geostar.constructionland.analyse.service.BaseAdjustService;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import com.geostar.constructionland.common.entity.response.LandInfo;
import com.geostar.constructionland.common.entity.response.model.AdjustModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author chengan
 * @description 在披红线分析
 */
@Slf4j
@Component
public class ZphxHandler implements IResultHandler{
    @Autowired
    BaseAdjustService adjustService;

    @Autowired
    ObjectMapper objectMapper;

    @Autowired
    AlyResultZphxMapper zphxMapper;

    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws Exception {
        HandlerResult result = HandlerResult.createBySuccess();
        log.info("处理行政区占压返回数据,状态码:{}",httpResult.getCode());
        if(HttpStatus.SC_OK==httpResult.getCode()){
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存占压信息**/
            AlyResult alyResult = objectMapper.readValue(httpResult.getBody(),AlyResult.class);
            log.info("body:"+httpResult.getBody());
            if(HttpStatus.SC_OK == alyResult.getStatus()){
                /**如果成功,构建主体解析**/
                List<AlyResultZphx> alyResultZphxes = objectMapper.readValue(alyResult.getAnalysisResult().getResultRecords(),
                        new TypeReference<List<AlyResultZphx>>(){});
                if(alyResultZphxes.size()>0){
                    alyResultZphxes.forEach(zphx -> {
                        zphx.setFxid(UUID.randomUUID().toString().replaceAll("-",""));
                        zphx.setFxbs(analyseModel.getAlyId());
                    });
                    String newResult = objectMapper.writeValueAsString(alyResultZphxes);
                    alyResult.getAnalysisResult().setResultRecords(newResult);

                    /**执行平差**/
                    AdjustModel adjustModel = AdjustModelFactory.getAdjustMod(analyseModel,alyResult);
                    /**平差后的结果**/
                    List<LandInfo> landInfo_adjusted = adjustService.doAjust(analyseModel,adjustModel);
                    /**设置分析标示**/
                    for(AlyResultZphx  alyResultZphx: alyResultZphxes){
                        alyResultZphx.setFxbs(alyResult.getAnalyseId());
                        /**更新占压面积**/
                        List<LandInfo> filterLandInfos = landInfo_adjusted.stream().filter(landInfo ->
                                (landInfo.getDkid().equals(alyResultZphx.getDkbs()))).collect(Collectors.toList());
                        if(filterLandInfos.size()>0){
                            alyResultZphx.setZymj(filterLandInfos.get(0).getZymj_adjusted());
                        }
                        zphxMapper.insert(alyResultZphx);
                    }
                }
            }else{
                throw new Exception(alyResult.getMessage());
            }
        }
        return result;
    }
}
